package uform.ui.assembler;
import thx.error.Error;
import thx.validation.EmailValidator;
import thx.validation.SingleLineValidator;
import thx.validation.StringLengthValidator;
import uform.ui.comp.Component;
import uform.ui.comp.StringControl;
import uform.ui.comp.StringKind;
import uform.model.value.StringType;
using Types;

class StringTypeAssembler extends BaseAssembler<StringType>
{
	override public function assemble(type : StringType) : Component
	{
		var options = _options(type);
		if (null != options)
			return options;
//		var model = this.form.model;
		var kind = Multiline;
		if (type.meta.exists("input-type"))
		{
			switch(type.meta.get("input-type"))
			{
				case "password":
					kind = Password;
				case "file":
					kind = File(type.meta.get("mime-types"));
				case "email":
					kind = Email(type.meta.get("email-validate-domain") == true);
				case "multiline":
					kind = Multiline;
				case "singleline":
					kind = Singleline;
				case "url":
					kind = Url;
				default:
					throw new Error("unknow input-type '{0}'", type.meta.get("input-type"));
			}
		} else for (validator in type.validators)
		{
			validator.ifIs(EmailValidator, function(v) {
				kind = Email(v.validateDomain);
			});
			validator.ifIs(SingleLineValidator, function(v) {
				kind = Singleline;
			});
		}
		var control = new StringControl("", kind);
		for (validator in type.validators)
		{
			validator.ifIs(StringLengthValidator, function(v) {
				control.maxLength.set(v.maxLength);
			});
		}
//		control.maxLength.set(type.maxLength);
		_bindBidirectional(control);
// 	type.minLength
		return control;
	}
}